package com.zzxz.practicaltrainingsys.service.impl;


import com.zzxz.practicaltrainingsys.entity.*;
import com.zzxz.practicaltrainingsys.mapper.ProjectMapper;
import com.zzxz.practicaltrainingsys.mapper.TopicMapper;
import com.zzxz.practicaltrainingsys.service.ProjectService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private TopicMapper topicMapper;

    @Override
    public List<Project> getProject() {
        return projectMapper.getProject();
    }

    @Override
    public List<String> getProjectNames() {
        return projectMapper.getProjectNames();
    }

    @Override
    public Map<String, Object> getProjectTime(Integer projectId) {
        if (projectId == null) {
            throw new IllegalArgumentException("项目ID不能为空");
        }
        Map<String, Object> projectTime = projectMapper.getProjectTime(projectId);
        return projectTime;
    }

    @Override
    public Project getProjectById(Integer project_id) {
        return projectMapper.getProjectById(project_id);
    }


    @Override
    public int insertProject(Project project) {
        projectMapper.insertProject(project); // 插入数据
        return project.getProject_id();      // 返回回填的 project_id
    }

    @Override
    public int updateProject(Project project) {
        // 添加详细的调试日志
        System.out.println("=== 开始更新项目 ===");
        System.out.println("项目ID: " + project.getProject_id()); // 使用下划线命名
        System.out.println("项目名称: " + project.getProject_name());
        System.out.println("项目简介: " + project.getProject_introduction());
        System.out.println("开始时间: " + project.getProject_start());
        System.out.println("结束时间: " + project.getProject_end());

        // 验证必要参数
        if (project.getProject_id() == null) {
            throw new IllegalArgumentException("项目ID不能为空");
        }

        // 先查询项目是否存在
        Project existingProject = projectMapper.getProjectById(project.getProject_id());
        if (existingProject == null) {
            System.out.println("错误：未找到ID为 " + project.getProject_id() + " 的项目");
            return 0;
        }

        System.out.println("找到现有项目: " + existingProject.getProject_name());

        // 执行更新
        int result = projectMapper.updateProject(project);
        System.out.println("数据库更新结果: " + result + " 行受影响");

        // 验证更新结果
        if (result > 0) {
            Project updatedProject = projectMapper.getProjectById(project.getProject_id());
            System.out.println("更新后的项目名称: " + updatedProject.getProject_name());
        }

        System.out.println("=== 更新项目完成 ===");
        return result;

    }

    @Override
    public int deleteProject(Project project) {
        return projectMapper.deleteProject(project);
    }


    @Override
    public School getSchoolById(Integer school_id) {
        return projectMapper.getSchoolById(school_id);

    }

    @Override
    public College getCollegeById(Integer college_id) {
        return projectMapper.getCollegeById(college_id);
    }

    @Override
    public Major getMajorById(Integer major_id) {
        return projectMapper.getMajorById(major_id);
    }

    @Override
    public Grade getGradeById(Integer grade_id) {
        return projectMapper.getGradeById(grade_id);
    }

    @Override
    public List<Project> getProjectByTeacherId(Integer teacher_id) {
        return projectMapper.getProjectByTeacherId(teacher_id);
    }

    @Override
    public List<Project> getProjectsByIds(Integer school_id, Integer college_id, Integer major_id, Integer grade_id) {
        return projectMapper.getProjectsByIds(school_id, college_id, major_id, grade_id);
    }

    // 获取当前正在实训的学生数量
    @Override
    public Integer getNowStudents() {
        return projectMapper.getNowStudents();
    }

    @Override
    public List<Topic> getTopicsByProjectId(Integer project_id) {
        return topicMapper.getTopicsByProjectId(project_id);
    }


    @Override
    public List<Project> getProjectsByStudentInfo(Integer schoolId, Integer collegeId, Integer majorId, Integer gradeId) {
        return projectMapper.getProjectsByStudentInfo(schoolId, collegeId, majorId, gradeId);
    }

    @Override
    public Project getProjectByProjectId(Integer projectId) {
        return projectMapper.getProjectByProjectId(projectId);
    }


}
